import Http from "../../../utils/http.js";
import WxValidate from '../../../utils/WxValidate';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vehicleId:"",
        //车主类型
        carType: 0,
        carTypeRange: [{
            name: '个人',
            value: '0',
            checked: 'true'
          },
          {
            name: '企业',
            value: '1',
            disabled: true
          }],

    identityFrontImg: "",
    identityFrontImgFullSrc: "",

    identityBackImg: "",
    identityBackImgFullSrc: "",

    licenseMainImg: "",
    licenseMainImgFullSrc: "",

    licenseSubImg: "",
    licenseSubImgFullSrc: "",

    registration12Img: "",
    registration12ImgFullSrc: "",

    registration34Img: "",
    registration34ImgFullSrc: "",



    commerciaInsuranceImg: "",
    commerciaInsuranceImgFullSrc: "",

    mandatoryInsuranceImg: "",
    mandatoryInsuranceImgFullSrc: "",

    corporationCertImg:"",
    corporationCertImgFullSrc:"",

    fixedAssetCertImg:"",
    fixedAssetCertImgFullSrc:"",
    vatInvoiceImg:"",
    vatInvoiceImgFullSrc:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options",options)
    this.setData({
      vehicleId:options.vehicleId
    });
    this._initValidate();
  },
  /**
   * 车主类型选择-改变事件
   */
  bindCarTypeChange: function (e) {
    console.log('bindCarTypeChange，携带值为', e.detail.value)
    this.setData({
      carType: e.detail.value
    })
  },
  bindUploadCarImg: function (e) {
    var dataSet = e.currentTarget.dataset;
    var that = this;
    console.log("bindUploadCarImg", dataSet);
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        wx.showLoading({
          title: ''
        });
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0], //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success: (res) => {
            let baseImg = 'data:image/png;base64,' + res.data
            Http.request({
              url: "/image/save",
              method: "POST",
              data: {
                image: baseImg
              },
              success: function (res) {
                console.log(res);
                let fullSrc = Http.apiUrl + Http.imgUrl + "?imagePath=" + res +
                  "&" + wx.getStorageSync("sessionid");
                //左前
                if (dataSet.name == "identityFrontImg") {
                  that.setData({
                    identityFrontImg: res,
                    identityFrontImgFullSrc: fullSrc,
                  })
                } else if (dataSet.name == "identityBackImg") {
                  //右后
                  that.setData({
                    identityBackImg: res,
                    identityBackImgFullSrc: fullSrc
                  })
                } else if (dataSet.name == "licenseMainImg") {
                  //右后
                  that.setData({
                    licenseMainImg: res,
                    licenseMainImgFullSrc: fullSrc
                  })
                } else if (dataSet.name == "licenseSubImg") {
                  //座椅
                  that.setData({
                    licenseSubImg: res,
                    licenseSubImgFullSrc: fullSrc
                  })
                } else if (dataSet.name == "registration12Img") {
                  //后备箱
                  that.setData({
                    registration12Img: res,
                    registration12ImgFullSrc: fullSrc
                  })
                } else if (dataSet.name == "registration34Img") {
                  that.setData({
                    registration34Img: res,
                    registration34ImgFullSrc: fullSrc
                  })
                } else if (dataSet.name == "commerciaInsuranceImg") {
                  that.setData({
                    commerciaInsuranceImg: res,
                    commerciaInsuranceImgFullSrc: fullSrc
                  })
                } else if (dataSet.name == "mandatoryInsuranceImg") {
                  that.setData({
                    mandatoryInsuranceImg: res,
                    mandatoryInsuranceImgFullSrc: fullSrc
                  })
                }else if (dataSet.name == "corporationCertImg") {
                  that.setData({
                    corporationCertImg: res,
                    corporationCertImgFullSrc: fullSrc
                  })
                }else if (dataSet.name == "fixedAssetCertImg") {
                  that.setData({
                    fixedAssetCertImg: res,
                    fixedAssetCertImgFullSrc: fullSrc
                  })
                }else if (dataSet.name == "vatInvoiceImg") {
                  that.setData({
                    vatInvoiceImg: res,
                    vatInvoiceImgFullSrc: fullSrc
                  })
                }
              }
            })
          },
          fail: () => {
            wx.hideLoading();
          }
        })
      }
    })
  },

  bindPicDel:function(e){
    var dataSet=e.currentTarget.dataset;
    console.log('车辆所属地，携带值为', dataSet);
  
    if(dataSet.name=="identityFrontImg"){
      //左前
      this.setData({
        identityFrontImg:"",
        identityFrontImgFullSrc:""
      })
    }else if(dataSet.name=="identityBackImg"){
      //右后
      this.setData({
        identityBackImg:"",
        identityBackImgFullSrc:""
      })
    }else if(dataSet.name=="licenseMainImg"){
       //中控
       this.setData({
        licenseMainImg:"",
        licenseMainImgFullSrc:""
      })
    }else if(dataSet.name=="licenseSubImg"){
      //座椅
      this.setData({
        licenseSubImg:"",
        licenseSubImgFullSrc:""
      })
    }else if(dataSet.name=="registration12Img"){
      //发动机
      this.setData({
        registration12Img:"",
        registration12ImgFullSrc:""  
      })
    }else if(dataSet.name=="registration34Img"){
      //后备箱
      this.setData({
        registration34Img:"",
        registration34ImgFullSrc:""  
      })
    }else if(dataSet.name=="commerciaInsuranceImg"){
      this.setData({
        commerciaInsuranceImg:"",
        commerciaInsuranceImgFullSrc:""  
      })
    }else if(dataSet.name=="mandatoryInsuranceImg"){
      this.setData({
        mandatoryInsuranceImg:"",
        mandatoryInsuranceImgFullSrc:""  
      })
    }else if(dataSet.name=="corporationCertImg"){
      this.setData({
        corporationCertImg:"",
        corporationCertImgFullSrc:""  
      })
    } else if(dataSet.name=="fixedAssetCertImg"){
      this.setData({
        fixedAssetCertImg:"",
        fixedAssetCertImgFullSrc:""  
      })
    } else if(dataSet.name=="vatInvoiceImg"){
      this.setData({
        vatInvoiceImg:"",
        vatInvoiceImgFullSrc:""  
      })
    } 
  },
  bindUploadImgLoad:function(){
    wx.hideLoading();
  },

  _initValidate: function () {
    const rules = {
      identityFrontImg:{
        required: true
      },
      identityBackImg:{
        required: true
      },
      licenseMainImg:{
        required: true
      },
      licenseSubImg:{
        required: true
      },
      registration12Img:{
        required: true
      },
      registration34Img:{
        required: true
      },
      commerciaInsuranceImg:{
        required: true
      },
      mandatoryInsuranceImg:{
        required: true
      },
      corporationCertImg:{
        required: true
      },
      fixedAssetCertImg:{
        required: true
      },
      vatInvoiceImg:{
        required: true
      }
    };
    const messages = {
      identityFrontImg:{
        required: "请上传经办人证或委托人有效证件正面照"
      },
      identityBackImg:{
        required: "请上传经办人证或委托人有效证件反面照"
      },
      licenseMainImg:{
        required: "请上传车辆行驶证主页"
      },
      licenseSubImg:{
        required: "请上传车辆行驶证反副业"
      },
      registration12Img:{
        required: "请上传登记证1、2页"
      },
      registration34Img:{
        required: "请上传登记证3、4页"
      },
      commerciaInsuranceImg:{
        required: "请上传商业保险单照片"
      },
      mandatoryInsuranceImg:{
        required: "请上传交强保险照片"
      }
    }
    this.WxValidate = new WxValidate(rules, messages);
  },
  bindFormSubmit:function(e){
    const params = e.detail.value;
    console.log("bindFormSubmit",params)
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0];
      console.log(error)
      wx.showToast({
        title: error.msg,
        icon: 'none',
        duration: 2000
      })
    }else{
      Http.request({
        url:"/vehicleInfo/valuation/add",
        method:"POST",
        data:params,
        success:function(res){
          wx.showModal({
            title: '提示',
            content: '已提交审核，请耐心等待！',
            showCancel:false,
            confirmText:"我知道了",
            success (res) {
              if (res.confirm) {
                wx.navigateBack();
              }
            }
          })    
        }
      })
    }
  }
})